using System;
using System.Collections;
using System.Drawing;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Forms;
using System.Net;
using System.Net.Cache;
using System.IO;
using System.Web;
using System.Xml;

using ZoneFiveSoftware.Common.Visuals;
using ZoneFiveSoftware.SportTracks.UI.Forms;
using BENBUN.Properties;
using BENBUN.Utils;

namespace BENBUN.MetaStore
{
    public partial class OptionsView : UserControl
    {

        public OptionsView()
        {
            InitializeComponent();
            txtUsername.Text = Settings.Settings.ForumUsername;
            txtPassword.Text = Settings.Settings.ForumPassword;
            checkBox_Confirmation.Checked = Settings.Settings.WIWConfirmation;
            checkBox_LoadConfirmation.Checked = Settings.Settings.WIWConfirmation;
            checkBox_WIWAfterImport.Checked = Settings.Settings.WIWEnabled;
            labelToken.Text = "Token = " + Controller.GetInstance().SessionToken;
            setModeButton();

            if (txtPassword.Text.Length > 0 && txtUsername.Text.Length > 0)
            {
                buttonCheckDetails.Enabled = true;
            }
            else
            {
                buttonCheckDetails.Enabled = false;
            }
        }

        public IPageStatus Status
        {
            get { return null; }
        }

        public bool Modified
        {
            get { return false; }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void ShowPage(string bookmark)
        {
            bIgnoreCtrlChanges = true;
            bIgnoreCtrlChanges = false;

        }

        public bool HidePage()
        {
            return true;
        }

        public void ThemeChanged(ITheme visualTheme)
        {
            BackColor = visualTheme.Control;
        }

        public void UICultureChanged(CultureInfo culture)
        {
        }

        #region Event handlers



        #endregion

        private bool bIgnoreCtrlChanges = false;

        private void setModeButton()
        {
            if ((txtUsername.Text != "benbun") && (txtPassword.Text != "19051969"))
            {
                button1.Visible = false;
                button2.Visible = false;
                labelToken.Visible = false;
                panel6.Visible = false;
            }
            else
            {
                button1.Visible = true;
                button2.Visible = true;
                labelToken.Visible = true;
                panel6.Visible = true;
            }

            if (Settings.Settings.WIWModeTest)
            {
                button1.Text = "Test";
                button1.BackColor = Color.Red;
            }
            else
            {
                button1.Text = "Live";
                button1.BackColor = Color.Black;
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string url = Resources.ForumURL;
            System.Diagnostics.Process.Start(url);
        }

        private void txtUsername_TextChanged(object sender, EventArgs e)
        {
            Settings.Settings.ForumUsername = txtUsername.Text;
            setModeButton();
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            Settings.Settings.ForumPassword = txtPassword.Text;

            if (txtPassword.Text.Length > 0 && txtUsername.Text.Length > 0)
            {
                buttonCheckDetails.Enabled = true;
            }
            else
            {
                buttonCheckDetails.Enabled = false;
            }
        }

        private void checkBox_WIWEnabled_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_WIWAfterImport.Checked)
            {
                Settings.Settings.WIWEnabled = true;
                checkBox_Confirmation.Enabled = true;
                checkBox_LoadConfirmation.Enabled = true;
                txtUsername.Enabled = true;
                txtPassword.Enabled = true;
            }
            else
            {
                Settings.Settings.WIWEnabled = false;
                checkBox_Confirmation.Enabled = false;
                checkBox_LoadConfirmation.Enabled = false;
                txtPassword.Enabled = false;
                txtUsername.Enabled = false;
            }
        }

        private void checkBox_WIWConfirmation_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_Confirmation.Checked)
            {
                Settings.Settings.WIWConfirmation = true;
            }
            else
            {
                Settings.Settings.WIWConfirmation = false;
            }
        }

        private void checkBox_Autoload_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_LoadConfirmation.Checked)
            {
                Settings.Settings.WIWAutoLoad = true;
            }
            else
            {
                Settings.Settings.WIWAutoLoad = false;
            }
        }

        private void buttonCheckDetails_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            string s = "";

            string apiURL = Settings.Settings.URLBaseAPI + "user.php";
            apiURL = apiURL + "?u=" + txtUsername.Text + "&p=" + MD5Tool.GetMD5Hash(txtPassword.Text) + "&mode=token";

            try
            {
                doc.Load(apiURL);

                XmlNodeList nodes = doc.SelectNodes("/wiw/validate");

                string tempValid = "";
                string tempToken = "";

                foreach (XmlNode node in nodes)
                {
                    tempValid = node.Attributes["user"].Value.ToString();
                    tempToken = node.Attributes["token"].Value.ToString();
                }

                if (tempValid == "true")
                {
                    s = "GOOD";
                }
                else
                {
                    s = "BAD";
                }
            }
            catch (Exception)
            {

                s = "offline";
            }

            string response = "";
            switch (s)
            {
                case "offline":
                    response = "No connection to WIW server.";
                    break;
                case "GOOD":
                    response = "Username and password have been validated.";
                    break;
                case "BAD":
                    response = "Invalid username/password details.";
                    break;
                default:
                    break;
            }

            MessageBox.Show(response, "User Details Validation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            string url = Resources.ForumURL;
            System.Diagnostics.Process.Start(url);
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            string url = Resources.TwitterURL;
            System.Diagnostics.Process.Start(url);
        }

        private void label4_Click(object sender, EventArgs e)
        {
            string url = Resources.BENBUNURL;
            System.Diagnostics.Process.Start(url);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Settings.Settings.WIWModeTest = !Settings.Settings.WIWModeTest;
            Controller.GetInstance().ClearToken();
            labelToken.Text = "Token = " + Controller.GetInstance().SessionToken;
            setModeButton();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Controller.GetInstance().ClearToken();
            labelToken.Text = "Token = " + Controller.GetInstance().SessionToken;
        }

      
    }
}